package com.aaa.mbs.controller;

import com.aaa.common.util.Result;
import com.aaa.common.util.ResultStatus;
import com.aaa.mbs.entity.Member;
import com.aaa.mbs.service.MemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Member)表控制层
 *
 * @author makejava
 * @since 2021-01-21 16:05:39
 */
@RestController
@RequestMapping("member")
@ApiOperation("member控制器")
public class MemberController {
    /**
     * 服务对象
     */
    @Resource
    private MemberService memberService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    @ApiOperation("单个对象查询")
    public Result selectOne(Integer id) {
        return new Result(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.memberService.queryById(id));
    }

    /**
     * 根据用户名获取用户信息
     * @param userName
     * @return
     */
    @GetMapping("getUserByUserName")
    public Member  getUserByUserName(@RequestParam("userName") String userName){
        return   this.memberService.getUserByUserName(userName);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    @PostMapping("queryAll")
    public Result queryAll(Integer pageNo, Integer pageSize, @RequestBody Member member) {
        return new Result(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.memberService.queryAll(pageNo, pageSize, member));
    }

    /**
     * 新增数据
     *
     * @param member 实例对象
     * @return 实例对象
     */
    @PostMapping("insert")
    public Result insert(@RequestBody Member member) {
        return new Result(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.memberService.insert(member));
    }

    /**
     * 修改数据
     *
     * @param member 实例对象
     * @return 实例对象
     */
    @PutMapping("update")
    public Result update(@RequestBody Member member) {
        return new Result(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.memberService.update(member));
    }

    /**
     * 通过主键删除数据
     *
     * @param memberId 主键
     * @return 是否成功
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer memberId) {
        return new Result(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.memberService.deleteById(memberId));
    }
}